package com.gh.pattern.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//Spring中的做法，就是用这种注册式单例
public class BeanFactory {

    private BeanFactory(){

    }
    //用ConcurrentHashMap即可实现线程安全
    private static Map<String,Object> ioc = new ConcurrentHashMap<String,Object>();

    //这里得用synchronized
    public synchronized static Object getBean(String className) {
        if (!ioc.containsKey(className)) {
            Object obj = null;
            try {
                obj = Class.forName(className).newInstance();
                ioc.put(className,obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return obj;
        }else {
            return ioc.get(className);
        }
    }
}
